// dep
var logger = bn.logger,
	_ = require('underscore');

// service of valid
var registry = {
	global : require('./Global'),
    article : require('./Article'),
    tag : require('./Tag'),
    category : require('./Category')
}

module.exports = function (app , config) {
	_.each(registry ,function(value , key ){
        var mappings = value.mappings;
		if(mappings){
			if(_.isArray(mappings)){
				_.each(mappings,function(mapping){
					reg(mapping ,key);
					logger.debug(key ,'service :', mapping.method, '/services/' + mapping.url ,' loaded.');
				})
			}else {
				var mapping = mappings;
				reg(mapping ,key);
				logger.debug(key ,'service :', mapping.method, '/services/' + mapping.url ,' loaded.');
			}
		}else{
			logger.warn(key , 'not found mappings object.');
		}
	});

    function reg(mapping ,key){
        if(!_.isObject(mapping)){
            throw new Error("error service mapping object" + 'on ' + key);
        }
        if(!_.isString(mapping['method'])){
            throw new Error("error service mapping method" + 'on ' + key);
        }
        if(!_.isString(mapping['url'])){
            throw new Error("error service mapping url" + 'on ' + key);
        }
        if(!_.isFunction(mapping['handler'])){
            throw new Error("error service mapping handler" + 'on ' + key);
        }
        (app[mapping.method])('/services/' + mapping.url , mapping.handler);
    }
};